﻿using Org.BouncyCastle.Asn1.X509;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace 基于WPF的电缆采样数据分析系统.Views.CommonComponent
{
    /// <summary>
    /// MyCustomControl.xaml 的交互逻辑
    /// </summary>
    public partial class MyCustomControl : UserControl
    {

        // 定义依赖属性用于接收父级传入的值
        public static readonly DependencyProperty ParentValueProperty =
            DependencyProperty.Register("ParentValue", typeof(string), typeof(MyCustomControl), new PropertyMetadata(default(string), OnParentValueChanged));

        // 定义依赖属性用于接收ViewModel中的值
        public static readonly DependencyProperty ViewModelValueProperty =
            DependencyProperty.Register("ViewModelValue", typeof(string), typeof(MyCustomControl), new PropertyMetadata(default(string), OnViewModelValueChanged));

        // 定义一个属性用于控制使用哪个值
        public static readonly DependencyProperty UseParentValueProperty =
            DependencyProperty.Register("UseParentValue", typeof(bool), typeof(MyCustomControl), new PropertyMetadata(false, OnUseParentValueChanged));

        // 获取或设置父级传入的值
        public string ParentValue
        {
            get => (string)GetValue(ParentValueProperty);
            set => SetValue(ParentValueProperty, value);
        }

        // 获取或设置ViewModel中的值
        public string ViewModelValue
        {
            get => (string)GetValue(ViewModelValueProperty);
            set => SetValue(ViewModelValueProperty, value);
        }

        // 控制是否使用父级传入的值
        public bool UseParentValue
        {
            get => (bool)GetValue(UseParentValueProperty);
            set => SetValue(UseParentValueProperty, value);
        }

        public MyCustomControl()
        {
            InitializeComponent();
            UpdateDisplayText();
        }

        private static void OnParentValueChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            var control = d as MyCustomControl;
            if (control != null && control.UseParentValue)
            {
                control.UpdateDisplayText();
            }
        }

        private static void OnViewModelValueChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            var control = d as MyCustomControl;
            if (control != null && !control.UseParentValue)
            {
                control.UpdateDisplayText();
            }
        }

        private static void OnUseParentValueChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            var control = d as MyCustomControl;
            if (control != null)
            {
                control.UpdateDisplayText();
            }
        }

        private void UpdateDisplayText()
        {
            DisplayText.Text = UseParentValue ? ParentValue : ViewModelValue;
        }
    }
}
